package com.jz.netthings.appengine.queue;

import com.jz.netthings.appengine.baseData.Location;
import com.jz.netthings.appengine.mapper.LocationMapper;
import io.netty.channel.ChannelHandlerContext;

import java.util.HashMap;
import java.util.List;

/**
 * 平板座椅管理
 */
public class ChairMgr {


    private static class SingletonHolder {

        private static ChairMgr	INSTANCE	= new ChairMgr();
    }

    public static ChairMgr getInstance() {
        return SingletonHolder.INSTANCE;
    }

    /**
     * 记录机器号和座椅号的关联关系
     */
    private HashMap<String, String> chairMacMap	= new HashMap<String, String>();
    /**
     * 记录平板和通道上下文
     */
    private HashMap<String, ChannelHandlerContext> ctxMap	= new HashMap<String, ChannelHandlerContext>();
    private HashMap<ChannelHandlerContext,String> ctxMap_1	= new HashMap<ChannelHandlerContext,String>();
    /**
     * 根据机器号，获取座椅号
     * @param mac
     * @return
     */
    public String getChairByMac(String mac) {
        return chairMacMap.get(mac);
    }

    /**
     * 根据座椅号，获取机器号
     * @param chair
     * @return
     */
    public String getMacByChair(String chair) {
        return chairMacMap.get(chair);
    }

    public void initChair(LocationMapper locationMapper){

        synchronized (ChairMgr.getInstance()){
            System.out.println("初始化 ChairMgr...");
            chairMacMap.clear();
            ctxMap.clear();
            ctxMap_1.clear();

            List<Location> locations = locationMapper.selectList(null);
            if (locations != null && locations.size()>0){
                for (Location loc :
                        locations) {
                    chairMacMap.put(loc.getMaccode(), loc.getChaircode());
                    chairMacMap.put(loc.getChaircode(),loc.getMaccode());
                }
            }


        }

    }

    /**
     * 根据平板号，获取通道
     * @param chair
     */
    public ChannelHandlerContext getCtxByChair(String chair){
        synchronized (this){
            return this.ctxMap.get(chair);
        }
    }
    /**
     * 根据机器号，获取机器对应的平板的通道
     * @param mcode
     */
    public ChannelHandlerContext getCtxByChairsMcode(String mcode){
        synchronized (this){
            String chair = this.getChairByMac(mcode);
            return this.ctxMap.get(chair);
        }
    }
    /**
     * 根据平板通道，获取机器号
     * @param ctx
     */
    public String getMacByChairCtx(ChannelHandlerContext ctx){
        synchronized (this){
            String chair = this.ctxMap_1.get(ctx);
            return this.getMacByChair(chair);
        }
    }
    /**
     * 根据平板通道，获取平板号
     * @param ctx
     */
    public String getChairByChairCtx(ChannelHandlerContext ctx){
        synchronized (this){
            return this.ctxMap_1.get(ctx);
        }
    }

    /**
     * 绑定平板和通道上下文
     * @param chair
     * @param ctx
     */
    public void bindCtxChair(String chair,ChannelHandlerContext ctx){
        synchronized (this) {
            this.cancelBind(chair);

            ctxMap.put(chair,ctx);
            ctxMap_1.put(ctx,chair);
        }
    }

    /**
     * 解绑平板和通道
     * @param chair
     */
    private void cancelBind(String chair){
        ChannelHandlerContext context = this.ctxMap.get(chair);
        if (context != null){
            System.out.println("移除原来绑定的平板和通道：【"+chair+"】【"+context.channel().remoteAddress()+"】");
            ctxMap_1.remove(context);
            ctxMap.remove(chair);
            context.channel().close();
            context.close();
        }
    }

}
